
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';

import '../../model/cartInfo.dart';
import '../../provide/cart.dart';
class CartItem extends StatelessWidget {
  final CardInfoModel item;
  const CartItem(this.item,{Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(180),
      margin: EdgeInsets.fromLTRB(5.0, 2.0, 5.0, 2.0),
      padding: EdgeInsets.fromLTRB(5.0,10.0,5.0,10.0),
      decoration: BoxDecoration(
        color:Colors.white,
        border: Border(
          bottom: BorderSide(color: Colors.black12,width: 1)
        )
      ),
      child:Row(
        children: <Widget>[
          _cardCheckBt(context),
          _cardImage(),
          _cardGoodsname(),
          _cardPrice(context),
        ],
      ) ,
    );
  }
  //复选按钮
  Widget _cardCheckBt(context){
    return Container(
      child: Checkbox(
        value: item.isCheck,
        activeColor: Colors.pink,
        onChanged: (bool val){
          item.isCheck = val;
          Provide.value<CartProvide>(context).changeCheckStateorCount(item);
        },
      ),
    );
  }
  //商品图片
  Widget _cardImage(){
    return Container(
      height: ScreenUtil().setHeight(150.0),
      width: ScreenUtil().setWidth(150.0),
      padding: EdgeInsets.all(3.0),
      decoration: BoxDecoration(
        border:Border.all(
          width: 1,color: Colors.black12
        )
      ),
      child: Image.network(
        item.images
      ),
    );
  }
  //商品名称
  Widget _cardGoodsname(){
    return Expanded(
      child:Container(
        padding: EdgeInsets.all(3.0),
        margin: EdgeInsets.only(left: 10.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(item.goodsName),
            CardCount(item)
          ],
        ),
      ) ,
    );
  }
  //商品价格
  Widget _cardPrice(context){
    return Container(
      width: ScreenUtil().setWidth(130.0),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Text(
            '￥${item.price}',
            style: TextStyle(color: Colors.black,fontSize: ScreenUtil().setSp(26.0)),
          ),
          Container(
            alignment: Alignment.centerRight,
            margin: EdgeInsets.only(top:10.0),
            child: InkWell(
              onTap:(){
                Provide.value<CartProvide>(context).deletOneGoods(item.goodsId);
              },
              child: Icon(Icons.delete,color:Colors.black26,size: 26),
            ),
          )
        ],
      ),
    );
  }
}

//商品数量
class CardCount extends StatelessWidget {
  final CardInfoModel item;
  const CardCount(this.item,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(200.0),
      decoration: BoxDecoration(
        border: Border.all(width: 1.0,color: Colors.black12)
      ),
      child: Row(
        children: <Widget>[
          _reduceBtn(context),
          _countArea(),
          _addBtn(context)
        ],
      ),
    );
  }

  //减少按钮
  Widget _reduceBtn(context){
    return InkWell(
      onTap: (){
        if(item.count > 1){
         item.count  = item.count -1;
         Provide.value<CartProvide>(context).changeCheckStateorCount(item);
        }
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(55.0),
        height: ScreenUtil().setHeight(45.0),
        decoration: BoxDecoration(
          color: item.count>1 ? Colors.white :Colors.black12,
          border: Border(right: BorderSide(width:1,color: Colors.black12 ))
        ),
        child:Text( item.count>1 ?'—':'') ,
      ),
    );
  }
  //加按钮
  Widget _addBtn(context){
    return InkWell(
      onTap: (){
         item.count  = item.count + 1;
         Provide.value<CartProvide>(context).changeCheckStateorCount(item);
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenUtil().setWidth(55.0),
        height: ScreenUtil().setHeight(45.0),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(left: BorderSide(width:1,color: Colors.black12 ))
        ),
        child:Text('+') ,
      ),
    );
  }
  //中间显示数字
  Widget _countArea(){
    return Expanded(
      child:Container(
        height: ScreenUtil().setHeight(45.0),
        alignment: Alignment.center,
        color: Colors.white,
        child: Text('${item.count}'),
      ) ,
    );
  }
}
